{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37232bitda0b5ea56bd841ef825ead3d06e7c5cd",
   "display_name": "Python 3.7.2 32-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "[5 8 8 4 9]\na    5\nb    8\nc    8\nd    4\ne    9\ndtype: int32\nc    8\nd    4\ne    9\ndtype: int32\nc    8\nd    4\ne    9\ndtype: int32\n"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "data1 = np.random.randint(3,10,5)\n",
    "print(data1)\n",
    "s1 = pd.Series(data1,index=list('abcde'))\n",
    "print(s1)\n",
    "print(s1[-3:])\n",
    "print(s1['c':'e'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "year  price\n0  2017     10\n1  2018     20\n2  2019     30\n"
    }
   ],
   "source": [
    "dic={'year':[2017,2018,2019],'price':[10,20,30]}\n",
    "dataf=pd.DataFrame(dic,index=list('012'))\n",
    "print(dataf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "one  two  three\na   13    6      7\nc    8    9      6\ne    5   13     14\nf   11   10      8\nh    8    6     10\n"
    }
   ],
   "source": [
    "data2=np.random.randint(5,15,(5,3))\n",
    "data_test=pd.DataFrame(data2,index=list('acefh'),columns=['one','two','three'])\n",
    "print(data_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "one    two  three  four\na   NaN -100.0    7.0  test\nb   NaN    NaN    NaN   NaN\nc   8.0  -99.0  -99.0  test\nd   NaN    NaN    NaN   NaN\ne   5.0   13.0   14.0  test\nf  11.0   10.0    8.0  test\ng   NaN    NaN    NaN   NaN\nh   8.0    6.0   10.0  test\n"
    }
   ],
   "source": [
    "data_change=data_test\n",
    "data_change.loc['a','one']=None\n",
    "data_change.loc['c','two']=-99\n",
    "data_change.loc['c','three']=-99\n",
    "data_change.loc['a','two']=-100\n",
    "data_change.loc[:,'four']='test'\n",
    "data_change=data_change.reindex(list('abcdefgh'))\n",
    "print(data_change)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "one   two  three  four\nc   8.0 -99.0  -99.0  test\ne   5.0  13.0   14.0  test\nf  11.0  10.0    8.0  test\nh   8.0   6.0   10.0  test\n"
    }
   ],
   "source": [
    "print(data_change.dropna()) #删除 df_change 中存在缺失值的所有行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "one    two  three  four\na   NaN -100.0    7.0  test\nc   8.0  -99.0  -99.0  test\ne   5.0   13.0   14.0  test\nf  11.0   10.0    8.0  test\nh   8.0    6.0   10.0  test\n"
    }
   ],
   "source": [
    "print(data_change.dropna(how='all')) #删除 df_change 中所有值都为 NaN 值的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "one    two  three  four\na   0.0 -100.0    7.0  test\nb   0.0    0.0    0.0     0\nc   8.0  -99.0  -99.0  test\nd   0.0    0.0    0.0     0\ne   5.0   13.0   14.0  test\nf  11.0   10.0    8.0  test\ng   0.0    0.0    0.0     0\nh   8.0    6.0   10.0  test\n"
    }
   ],
   "source": [
    "print(data_change.fillna(0)) #df_change 中的所有缺失值（即 NaN）以 0 填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "one    two  three  four\na   NaN -100.0    7.0  test\nb   NaN    NaN    NaN   NaN\nc   8.0  -99.0  -99.0  test\ne   5.0   13.0   14.0  test\nf  11.0   10.0    8.0  test\nh   8.0    6.0   10.0  test\n"
    }
   ],
   "source": [
    "print(data_change.drop_duplicates()) #删除 df_change中的重复行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}